package com.lzh.runke.controller.excel;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.StopWatch;
import com.lzh.runke.core.web_base.constant.DateConstant;
import com.lzh.runke.core.web_base.view.excel.AbstractExcelView;
import com.lzh.runke.entity.TWxfwZyld;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.poi.ss.usermodel.*;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @Author: LiuZiHao
 * @Date: 2022/12/15 23:09
 */
@Component("TransferCopyView")
public class TransferCopyView extends AbstractExcelView<TransferCopyView> {
    @Override
    protected void buildExcelDocument(Map<String, Object> model, Workbook workbook, HttpServletRequest request, HttpServletResponse response) throws Exception {
        StopWatch stopWatch = DateUtil.createStopWatch("poi导出");
        stopWatch.start("生成文件");
        Sheet sheet = workbook.getSheetAt(0);
        if (ObjectUtils.isEmpty(sheet)) {
            return;
        }
        Map map = (Map) model.get("result");
        if (ObjectUtils.isEmpty(map)) {
            return;
        }
        List<TWxfwZyld> dataList = (List<TWxfwZyld>) map.get("data");
        CellStyle cellStyle = this.buildStyleNone(workbook, HorizontalAlignment.CENTER, VerticalAlignment.CENTER);
        Font font = workbook.createFont();//获取字体设置对象
        font.setFontName("宋体");
        cellStyle.setFont(font);
        //添加数据
        if (CollectionUtils.isNotEmpty(dataList)) {
            int rowIndex = 0;
            int colIndex;
            for (TWxfwZyld data : dataList) {
                colIndex = 0;
                rowIndex += 1;
                Row row = sheet.getRow(rowIndex);
                if (ObjectUtils.isEmpty(row)) {
                    row = sheet.createRow(rowIndex);
                }
                addCell(row, colIndex++, String.valueOf(rowIndex), cellStyle);
                addCell(row, colIndex++, data.getLdbh(), cellStyle);
                addCell(row, colIndex++, data.getPwdw(), cellStyle);
                addCell(row, colIndex++, data.getXydm(), cellStyle);
                addCell(row, colIndex++, data.getHylb(), cellStyle);
                addCell(row, colIndex++, data.getYsdw(), cellStyle);
                addCell(row, colIndex++, data.getJydw(), cellStyle);
                addCell(row, colIndex++, data.getJyxkzh(), cellStyle);
                addCell(row, colIndex++, data.getJylb(), cellStyle);
                addCell(row, colIndex++, ObjectUtils.isEmpty(data.getCzfs()) ? "-" : data.getCzfs().getText(), cellStyle);
                addCell(row, colIndex++, data.getFwmc(), cellStyle);
                addCell(row, colIndex++, ObjectUtils.isEmpty(data.getFwlx()) ? "-" : data.getFwlx().getHazardouswasteTypeName(), cellStyle);
                addCell(row, colIndex++, ObjectUtils.isEmpty(data.getFwlx()) ? "-" : data.getFwlx().getHazardouswasteTypeCode(), cellStyle);
                addCell(row, colIndex++, data.getFwxt(), cellStyle);
                addCell(row, colIndex++, data.getSl(), cellStyle);
                addCell(row, colIndex++, data.getYsrq(), cellStyle);
                addCell(row, colIndex++, data.getYsxl(), cellStyle);
                addCell(row, colIndex++, data.getCph(), cellStyle);
                addCell(row, colIndex++, data.getJsr(), cellStyle);
                addCell(row, colIndex++, data.getJsrq(), cellStyle);
                addCell(row, colIndex, ObjectUtils.isEmpty(data.getLdzt()) ? "-" : data.getLdzt().getText(), cellStyle);
            }
        }

        StringBuilder fileName = new StringBuilder();
        String exportTime = LocalDateTime.ofInstant(new Date().toInstant(), ZoneId.systemDefault()).format(DateTimeFormatter.ofPattern(DateConstant.FMT_YMD));
        fileName.append("固体废物").append("-").append(exportTime);
        setExportFilename(response, fileName + XLSX);
        stopWatch.stop();
    }
}
